/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.adama.service;

import static fr.adama.constante.Constante.*;
import fr.adama.dto.ContratDTO;
import fr.adama.repository.ContratRepository;
import java.io.File;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author avisto
 */
@Service
@Transactional
public class ContratService extends CrudService {

    private static final Logger logger = LoggerFactory.getLogger(ContratService.class);
    @Autowired
    ContratRepository contratRepository;
    @Value("${outputFolder}")
    String outputFolder;

    public ContratDTO add(ContratDTO entityDTO, Class entity) {
        Long contratId = super.add(entityDTO, entity);
        entityDTO.setId(contratId);
        File outputDirectory = new File(outputFolder);

        File directoryAdherent = new File(outputDirectory.getAbsoluteFile() + "/" + entityDTO.getAdherent().getId());
        File directoryEmploye = new File(directoryAdherent.getAbsoluteFile() + "/" + entityDTO.getEmploye().getId());
        File directoryBulletinPaie = new File(directoryEmploye.getAbsoluteFile() + "/" + pathBulletin);
        File directoryBulletinFacture = new File(directoryEmploye.getAbsoluteFile() + "/" + pathFacture);

        directoryBulletinPaie.mkdirs();
        directoryBulletinFacture.mkdirs();
        logger.info("Création des répertoires du contrat.id={} bulletin={} facture={}", new Object[]{entityDTO.getId(), directoryBulletinPaie.getAbsolutePath(), directoryBulletinFacture.getAbsolutePath()});
        return entityDTO;

    }
}
